#include "stdafx.h"
#include "jmainframe.h"
#include "WebViewMiniBlink.h"
#include "msado15_tlh.h"
#include <wx/msgdlg.h>
#include "jinpatient.h"
#include "jglobals.h"
#include "jbleditor.h"



bool JMainFrame::register_page(const std::wstring &lsh,wxWindow * pwnd)
{
    auto pos=this->m_pages.left.find(lsh);
    auto pos2=this->m_pages.right.find(pwnd);
    if(pos==this->m_pages.left.end() && pos2==this->m_pages.right.end())
    {
        pwnd->Connect( wxEVT_DESTROY,wxWindowDestroyEventHandler( JMainFrame::OnPageDestroy ), NULL, this );
        m_pages.left.insert({lsh,pwnd});
        return true;
    }else
    {
        return false;
    }
}

bool JMainFrame::unregister_page(const std::wstring &lsh)
{
    auto pos=m_pages.left.find(lsh);
    if(pos!=m_pages.left.end())
    {
        m_pages.left.erase(lsh);
        return true;
    }
    return true;
}

bool JMainFrame::unregister_page(wxWindow *pwnd)
{
    auto pos=m_pages.right.find(pwnd);
    if(pos!=m_pages.right.end())
    {
        m_pages.right.erase(pwnd);
        return true;
    }
    return true;
}

bool JMainFrame::has_page(const std::wstring &lsh) const
{
    auto pos=m_pages.left.find(lsh);
    if(pos!=m_pages.left.end())
    {
        return true;
    }
    return true;
}

bool JMainFrame::has_page(wxWindow *pwnd) const
{
    auto pos=m_pages.right.find(pwnd);
    if(pos!=m_pages.right.end())
    {
        return true;
    }
    return true;
}

std::wstring JMainFrame::get_page_key(wxWindow *pwnd)
{
    auto pos=m_pages.right.find(pwnd);
    if(pos==m_pages.right.end())
    {
        return std::wstring();
    }else
    {
        return pos->second;
    }
}

wxWindow *JMainFrame::get_page_from_key(const std::wstring &key)
{
    auto pos=m_pages.left.find(key);
    if(pos==m_pages.left.end())
    {
        return NULL;
    }else
    {
        return pos->second;
    }
}

void JMainFrame::on_create_bleditor(const std::wstring &xm, const std::wstring &blh, const std::wstring &lsh)
{
    std::wstring key=std::wstring(L"病历编辑:")+blh;
    JBLEditor * editor=(JBLEditor*)this->get_page_from_key(key);
    if(editor==NULL)
    {
        editor=new JBLEditor(this->getNoteBook(),lsh);
        this->register_page(key,editor);
        this->m_notebook->AddPage(editor,xm,true);
    }else
    {
        int nn=m_notebook->GetPageIndex(editor);
        if(nn>=0)
        {
            m_notebook->SetSelection(nn);
        }
    }
}
void JMainFrame::OnPageDestroy(wxWindowDestroyEvent &event)
{
    event.Skip(true);
    wxWindow * pwnd=event.GetWindow();
    pwnd->Disconnect( wxEVT_DESTROY,wxWindowDestroyEventHandler( JMainFrame::OnPageDestroy ), NULL, this );
    this->m_pages.right.erase(pwnd);
}

void JMainFrame::OnCommandZYBR(wxCommandEvent &event)
{
    wxWindow * pwnd=this->get_page_from_key(PAGE_KEY_ZYBR);
    if(pwnd==NULL)
    {
        pwnd=new JInPatient(this->m_notebook);
        this->register_page(PAGE_KEY_ZYBR,pwnd);
        m_notebook->AddPage(pwnd,PAGE_KEY_ZYBR,true);
    }else
    {
        int nn=m_notebook->GetPageIndex(pwnd);
        if(nn>=0)
        {
            m_notebook->SetSelection(nn);
        }
    }

}
JMainFrame::JMainFrame(wxWindow* parent)
    :JMainFrameUI(parent)
{
    JGlobals::SetValue<JMainFrame*>(L"MainFrame",this);
}

JMainFrame::~JMainFrame()
{
    JGlobals::Erase(L"MainFrame");
}

void JMainFrame::OnCloseMainFrame(wxCloseEvent &event)
{
    if(wxYES==wxMessageBox(wxT("是否退出应用程序?"),wxT("EMRHelper"),wxYES_NO|wxCENTRE,this))
    {
        event.Skip(true);
    }else {
        event.Skip(false);
    }

}

void JMainFrame::OnCommandExit(wxCommandEvent &event)
{
    this->Close();
}
